package com.example.service;

import com.example.controller.CarController;
import com.example.dao.BootDao;
import com.example.dao.CarDao;
import com.example.entity.BootEntity;
import com.example.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private BootDao dao;
    @Autowired
    private CarDao carDao;

    //@Autowired
    //private CarController carController;

    @Override
    public void insertBoot(BootEntity entity) {
        //先看看如果是因为约束问题，会抛出什么异常 MySQLIntegrityConstraintViolationException
        //org.springframework.dao.DuplicateKeyException:
        try {
            dao.insertBootEntity(entity);
        } catch (DuplicateKeyException e) {
            e.printStackTrace();
            throw ServiceException.createException("1117");
        }
         catch (Exception e) {
            e.printStackTrace();
            throw ServiceException.createException("1118");
        }
    }

    @Override
    public BootEntity getBootEntity(int id) {
        //由此判断，只有一个容器
        //System.out.println(carController.toString());
        return dao.getBootEntity(id);
    }

    @Override
    public void deleteBoot(int id) {
        dao.deleteBootEntity(id);
    }
    @Override
    @Transactional
    public void deleteBootAndCar(int id)
    {
        dao.deleteBootEntity(id);
        //dao.insertBootEntity(new BootEntity());
        carDao.deleteCarByBootId(id);
    }
    /*@Transactional
    public void insertBootAndCar(int id)
    {
        dao.insertBootEntity(id);
        carDao.deleteCarByBootId(id);
    }*/
}
